package com.lingjtx.common.redis.config;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.lang.generator.SnowflakeGenerator;
import com.lingjtx.common.core.constant.RedisConfig;
import com.lingjtx.common.redis.service.IRedisService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SnowflakeConfig {

    private final IRedisService redisService;

    public SnowflakeConfig(IRedisService redisService) {
        this.redisService = redisService;
    }

    @Bean
    public SnowflakeGenerator generator() {
        Long workerId = redisService.increment(RedisConfig.APP_WORKER_ID);
        return new SnowflakeGenerator(workerId % Snowflake.MAX_WORKER_ID, 0L);
    }

    public Long nextId() {
        return generator().next();
    }
}
